package data;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;

public class UserLevel extends AbstractData {
	
	private long id = -1;
	private long hotelId;
	private String title;
	private List<POS> workAreas;
	private String workAreasString;
	private POS defaultWorkAreas;
	private String defaultWorkAreasString;
	private boolean adminAllowed;
	private boolean housekeepingAllowed;
	private boolean shortHotelCode;
	private boolean status;
	private boolean avaliable;
	
	public UserLevel()
	{
		this.id = -1;
		this.status = true;
		this.avaliable = true;
	}
	public UserLevel(long id)
	{
		this.id = id;
		this.status = true;
		this.avaliable = true;
	}
	
	public void setId(long id) {
		this.id = id;
	}
	@Override
	public long getId() {
		return id;
	}
	public long getHotelId() {
		return hotelId;
	}
	public void setHotelId(long hotelId) {
		this.hotelId = hotelId;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public List<POS> getWorkAreas() {
		if ((workAreas == null || workAreas.size() == 0) && !(workAreasString == null || workAreasString.isEmpty())) {
			String[] idOfCounter = SplitList(workAreasString);
			List<POS> list = new ArrayList<POS>();
			for (String idCounter : idOfCounter) {
				POS c = new POS(Long.parseLong(idCounter));
				list.add(c);
			}
			
			for(POS c : list)
				c.LoadData();
			
			workAreas = list;
		}
		return workAreas;
	}
	public void setWorkAreas(List<POS> workAreas) {
		this.workAreasString = CombineList(workAreas);
		this.workAreas = workAreas;
	}
	public String getWorkAreasString() {
		return workAreasString;
	}
	public void setWorkAreasString(String workAreasString) {
		this.workAreasString = workAreasString;
	}
	public boolean isStatus() {
		return status;
	}
	public void setStatus(boolean status) {
		this.status = status;
	}
	public boolean isAvaliable() {
		return avaliable;
	}
	public void setAvaliable(boolean avaliable) {
		this.avaliable = avaliable;
	}
	
	public POS getDefaultWorkAreas() {
		if (defaultWorkAreas == null
				&& !(defaultWorkAreasString == null || defaultWorkAreasString.isEmpty())) {
			POS p = new POS(Long.parseLong(defaultWorkAreasString));
			p.LoadData();
			defaultWorkAreas = p;
		}
		return defaultWorkAreas;
	}
	public void setDefaultWorkAreas(POS defaultWorkAreas) {
		this.defaultWorkAreasString = String.format("%d", defaultWorkAreas.getId());
		this.defaultWorkAreas = defaultWorkAreas;
	}
	public String getDefaultWorkAreasString() {
		return defaultWorkAreasString;
	}
	public void setDefaultWorkAreasString(String defaultWorkAreasString) {
		this.defaultWorkAreasString = defaultWorkAreasString;
	}
	public boolean isShortHotelCode() {
		return shortHotelCode;
	}
	public void setShortHotelCode(boolean shortHotelCode) {
		this.shortHotelCode = shortHotelCode;
	}
	
	public boolean isAdminAllowed() {
		return adminAllowed;
	}
	
	public void setAdminAllowed(boolean adminAllowed) {
		this.adminAllowed = adminAllowed;
	}
	
	public boolean isHousekeepingAllowed() {
		return housekeepingAllowed;
	}
	
	public void setHousekeepingAllowed(boolean housekeepingAllowed) {
		this.housekeepingAllowed = housekeepingAllowed;
	}
	
	@Override
	public Entity getEntity() {
		Entity item;
		if(this.key == null)
			item = new Entity(EntityName.UserLevel);
		else
			item = new Entity(this.key);
		item.setProperty("hotelId", this.hotelId);
		item.setProperty("title", this.title);
		item.setProperty("workAreas", this.workAreasString);
		item.setProperty("adminAllowed", this.adminAllowed);
		item.setProperty("housekeepingAllowed", this.housekeepingAllowed);
		item.setProperty("status", this.status);
		item.setProperty("avaliable", this.avaliable);
		item.setProperty("shortHotelCode", this.shortHotelCode);
		item.setProperty("defaultWorkAreas", this.defaultWorkAreasString);
		return item;
	}
	@Override
	public void setEntity(Entity entity) {
		this.id = entity.getKey().getId();
		this.hotelId = (Long) entity.getProperty("hotelId");
		this.title = (String) entity.getProperty("title");
		this.workAreasString = (String) entity.getProperty("workAreas");
		this.adminAllowed = (Boolean) entity.getProperty("adminAllowed");
		this.housekeepingAllowed = (Boolean) entity.getProperty("housekeepingAllowed");
		this.status = (Boolean) entity.getProperty("status");
		this.avaliable = (Boolean) entity.getProperty("avaliable");
		this.shortHotelCode = (Boolean) entity.getProperty("shortHotelCode");
		this.defaultWorkAreasString = (String) entity.getProperty("defaultWorkAreas");
		
		this.key = entity.getKey();
	}

	public void LoadData()
	{
		if(this.id > -1)
		{
			Entity entity = LoadData(EntityName.UserLevel,this.id);
			setEntity(entity);
		}
	}
}
